package com.gimi.cloud.jimi.manage.consumer.service.prod.impl;

import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.product.SecurityInstructionsDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.SecurityInstructionsFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.prod.SecurityInstructionsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @auther loongs
 * @date 2018/1/29  17:04
 */
@Service
public class SecurityInstructionsServiceImpl implements SecurityInstructionsService {
    private static final Logger logger = LoggerFactory.getLogger(SecurityInstructionsServiceImpl.class);

    private SecurityInstructionsFeignService securityInstructionsFeignService;

    @Autowired
    public SecurityInstructionsServiceImpl(SecurityInstructionsFeignService securityInstructionsFeignService) {
        this.securityInstructionsFeignService = securityInstructionsFeignService;
    }


    @Override
    public Response getSecurityInstructionsPageList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        SecurityInstructionsDTO securityInstructionsDTO = JsonUtil.jsonToBean(requestBody, SecurityInstructionsDTO.class);
        EntityVerification.checkByGroupsThrowsException(securityInstructionsDTO, SecurityInstructionsDTO.SecurityInstructionsPageList.class);
        return securityInstructionsFeignService.getSecurityInstructionsPageList(securityInstructionsDTO);
    }

    @Override
    public Response getSecurityInstructionsSingle(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        SecurityInstructionsDTO securityInstructionsDTO = JsonUtil.jsonToBean(requestBody, SecurityInstructionsDTO.class);
        EntityVerification.checkByGroupsThrowsException(securityInstructionsDTO, SecurityInstructionsDTO.SecurityInstructionsSingle.class);
        return securityInstructionsFeignService.getSecurityInstructionsSingle(securityInstructionsDTO);
    }

    @Override
    public Response addOrEditSecurityInstructions(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        Map<String, String> map = JsonUtil.jsonToMap(requestBody);
        SecurityInstructionsDTO securityInstructionsDTO = JsonUtil.jsonToBean(requestBody, SecurityInstructionsDTO.class);
        if (StringUtil.isBlank(map.get("id"))) {
            EntityVerification.checkByGroupsThrowsException(securityInstructionsDTO, SecurityInstructionsDTO.AddSecurityInstructions.class);
        } else {
            EntityVerification.checkByGroupsThrowsException(securityInstructionsDTO, SecurityInstructionsDTO.EditSecurityInstructions.class);
        }
        return securityInstructionsFeignService.addOrEditSecurityInstructions(securityInstructionsDTO);
    }

    @Override
    public Response delSecurityInstructions(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        SecurityInstructionsDTO securityInstructionsDTO = JsonUtil.jsonToBean(requestBody, SecurityInstructionsDTO.class);
        EntityVerification.checkByGroupsThrowsException(securityInstructionsDTO, SecurityInstructionsDTO.DelSecurityInstructions.class);
        return securityInstructionsFeignService.delSecurityInstructions(securityInstructionsDTO);
    }

    @Override
    public Response fileUploadSecurityInstructions(HttpServletResponse response, HttpServletRequest request) throws Exception {
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        return securityInstructionsFeignService.fileUploadSecurityInstructions(multipartFiles);
    }
}
